package com.lz.myblog.controller;


import com.lz.myblog.entity.Articletypeinfo;
import com.lz.myblog.repository.ArticletypeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller //注解为请求控制处理
@RequestMapping("/articletype")
public class ArticletypeController {
    @Autowired
    ArticletypeRepository articletypeRepository;//注入对象

    @RequestMapping("/add")  //跳转到添加页面
    public String add(){


        return "articletypeAdd";
    }

    @RequestMapping("/save")
    public String save(Articletypeinfo articletypeinfo){
        articletypeRepository.save(articletypeinfo);
        return "redirect:list";
    }

    @RequestMapping("/edit")
    public String edit(int articletypeid, Model model){
        Articletypeinfo one = articletypeRepository.getOne(articletypeid);
        model.addAttribute("one",one);
        return "admin/articletypeUpdate";
    }
    @RequestMapping("/update")
    public String update(Articletypeinfo articletypeinfo){
        articletypeRepository.save(articletypeinfo);
        return "redirect:list";
    }

    @RequestMapping("/detail")
    public String detail(int articletypeid, Model model){
        Articletypeinfo one = articletypeRepository.getOne(articletypeid);
        model.addAttribute("one",one);
        return "admin/articletypeDetail";
    }

    @RequestMapping("/delete")
    public String delete(int articletypeid){
        articletypeRepository.deleteById(articletypeid);
        return "redirect:list";
    }

    @RequestMapping("/list")
    public String list(Model model, Integer p){
        int pageSize = 10; //每页显示10条数据
        p = p==null?1:p; //判断是否传递了页码
//        p= p-1;
        Sort sort = Sort.by(Sort.Direction.DESC,"articletypeid");  //排序条件
        Page<Articletypeinfo> all = articletypeRepository.findAll(PageRequest.of(p - 1, pageSize, sort));
        List<Articletypeinfo> content = all.getContent();
        int totalPages = all.getTotalPages();
        //绑定参数
        model.addAttribute("content",content);
        model.addAttribute("totalPages",totalPages);
        model.addAttribute("currenPage",p);
        return "admin/articletypeList";//跳转到jsp页面
    }
}
